//
//  HZL_LoginViewController.m
//  B_Project
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_LoginViewController.h"
#import "EaseMob.h"
#import "HZL_FirstMainViewController.h"
#import "AppDelegate.h"

@interface HZL_LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation HZL_LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-liebiao"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
}
- (IBAction)doRegister:(UIButton *)sender {
    // 异步注册
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.usernameTextField.text password:self.passwordTextField.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (!error) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    } onQueue:nil];
    
}
- (IBAction)doLogin:(UIButton *)sender {
    
    // 异步登陆
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.usernameTextField.text password:self.passwordTextField.text completion:^(NSDictionary *loginInfo, EMError *error) {
        if (!error && loginInfo) {
            // 设置是否自动登陆
            [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
            // 获取数据库中数据
            [[EaseMob sharedInstance].chatManager loadDataFromDatabase];
            // 获取数组列表
            [[EaseMob sharedInstance].chatManager asyncFetchMyGroupsList];
#warning 开始跳转, 然后开始聊天
            NSLog(@"登陆成功");
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    } onQueue:nil];
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UITabBarController *tab = app.tab;
    
    [self.sideMenuViewController setContentViewController:tab animated:YES];
    [self.sideMenuViewController hideMenuViewController];
    
}
//判断账号和密码是否为空
//- (BOOL)isEmpty{
//    BOOL ret = NO;
//    NSString *username = _usernameTextField.text;
//    NSString *password = _passwordTextField.text;
//    if (username.length == 0 || password.length == 0) {
//        ret = YES;
//        [EMAlertView showAlertWithTitle:NSLocalizedString(@"prompt", @"Prompt")
//                                message:NSLocalizedString(@"login.inputNameAndPswd", @"Please enter username and password")
//                        completionBlock:nil
//                      cancelButtonTitle:NSLocalizedString(@"ok", @"OK")
//                      otherButtonTitles:nil];
//    }
    
//    return ret;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
